<template>
  <div>
    <van-datetime-picker
      v-model="birthdayDate"
      type="date"
      title="选择年月日"
      :min-date="minDate"
      :max-date="maxDate"
      @cancel="$emit('close-popup')"
      @confirm="onConfirm"
    />
  </div>
</template>

<script>
import { updatetUserProfile } from '@/api/user.js'
import dayJs from 'dayjs'
export default {
  name: 'birthday',
  data () {
    return {
      birthdayDate: new Date(),
      minDate: new Date('1900-01-01'),
      maxDate: new Date()
    }
  },
  props: {
    value: {
      type: String,
      required: true
    }
  },
  created () {
    this.birthdayDate = new Date(this.value)
  },

  methods: {
    async onConfirm (value) {
      this.$toast.loading({
        message: '保存中...',
        forbidClick: true,
        duration: 0
      })
      const formatDate = dayJs(value).format('YYYY-MM-DD')
      try {
        await updatetUserProfile({
          birthday: formatDate
        })
        this.$emit('close-popup')
        this.$emit('input', formatDate)
        this.$toast.success('更新成功!')
      } catch (err) {
        this.$toast('更新失败!')
      }
    }
  }
}
</script>

<style scoped lang='less'>

</style>
